package cn.com.guage.dtm.metadata.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.guage.dtm.common.annotation.Log;
import cn.com.guage.dtm.common.enums.BusinessType;
import cn.com.guage.dtm.metadata.domain.MdField;
import cn.com.guage.dtm.metadata.service.IMdFieldService;
import cn.com.guage.dtm.common.core.controller.BaseController;
import cn.com.guage.dtm.common.core.domain.AjaxResult;
import cn.com.guage.dtm.common.utils.poi.ExcelUtil;
import cn.com.guage.dtm.common.core.page.TableDataInfo;

/**
 * 字段MD定义Controller
 * 
 * @author liujun
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/metadata/field")
public class MdFieldController extends BaseController
{
    private String prefix = "metadata/field";

    @Autowired
    private IMdFieldService mdFieldService;

//    @RequiresPermissions("metadata:field:view")
//    @GetMapping()
//    public String field()
//    {
//        return prefix + "/field";
//    }
    
    @RequiresPermissions("metadata:field:view")
    @GetMapping("/{tableId}")
    public String fieldOfTable(@PathVariable("tableId") Long tableId, ModelMap mmap)
    {
    	mmap.put("tableId", tableId);
        return prefix + "/field";
    }

    /**
     * 查询字段MD定义列表
     */
    @RequiresPermissions("metadata:field:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MdField mdField)
    {
        startPage();
        List<MdField> list = mdFieldService.selectMdFieldList(mdField);
        return getDataTable(list);
    }

    /**
     * 导出字段MD定义列表
     */
    @RequiresPermissions("metadata:field:export")
    @Log(title = "字段MD定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MdField mdField)
    {
        List<MdField> list = mdFieldService.selectMdFieldList(mdField);
        ExcelUtil<MdField> util = new ExcelUtil<MdField>(MdField.class);
        return util.exportExcel(list, "字段MD定义数据");
    }

    /**
     * 新增字段MD定义
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存字段MD定义
     */
    @RequiresPermissions("metadata:field:add")
    @Log(title = "字段MD定义", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MdField mdField)
    {
        return toAjax(mdFieldService.insertMdField(mdField));
    }

    /**
     * 修改字段MD定义
     */
    @GetMapping("/edit/{fieldId}")
    public String edit(@PathVariable("fieldId") Long fieldId, ModelMap mmap)
    {
        MdField mdField = mdFieldService.selectMdFieldById(fieldId);
        mmap.put("mdField", mdField);
        return prefix + "/edit";
    }

    /**
     * 修改保存字段MD定义
     */
    @RequiresPermissions("metadata:field:edit")
    @Log(title = "字段MD定义", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MdField mdField)
    {
        return toAjax(mdFieldService.updateMdField(mdField));
    }

    /**
     * 删除字段MD定义
     */
    @RequiresPermissions("metadata:field:remove")
    @Log(title = "字段MD定义", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mdFieldService.deleteMdFieldByIds(ids));
    }
}
